/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim.camera;

import camera.AbstractCamera;

import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;

/**
 *
 * @author hp
 */
public class SimCam extends AbstractCamera {

    private Camera cam;
    private final Vector3f Y = Vector3f.UNIT_Y;

    public SimCam(Camera cam) {
        this.cam = cam;
    }

    @Override
    public Camera getCam() {
        return cam;
    }

    public void updateCamera(Vector3f modelPosition) {
        modelPosition.setY(cam.getLocation().getY());
        Vector3f dir = modelPosition.subtract(cam.getLocation());
        dir.setY(cam.getLocation().getY());
        cam.lookAt(dir, Y);
        float distance = dir.distance(cam.getLocation());
        System.out.println("Distanceeeee = "+distance);
        if (distance < 1000) {
            zoom(1); // zoom out
        } else if (distance > 500) {
            zoom(-1); // zoom in
        }
    }

    protected void zoom(float value) {
        float h = cam.getFrustumTop();
        float w = cam.getFrustumRight();
        float aspect = w / h;

        float near = cam.getFrustumNear();

        float fovY = FastMath.atan(h / near)
                / (FastMath.DEG_TO_RAD * .5f);
        fovY += value * 0.1f;

        h = FastMath.tan(fovY * FastMath.DEG_TO_RAD * .5f) * near;
        w = h * aspect;
        System.out.println("Height"+h+"Width"+w+"FOVY: "+fovY+" VALUE: "+value);
        if ((fovY > 20 && value < 0) || (fovY < 50 && value > 0)) {
            cam.setFrustumTop(h);
            cam.setFrustumBottom(-h);
            cam.setFrustumLeft(-w);
            cam.setFrustumRight(w);
        }


    }

    @Override
    public Vector3f getLocation() {
        return cam.getLocation();
    }

    @Override
    public void setViewPort(float[] viewPortVals) {
        cam.setViewPort(viewPortVals[0], viewPortVals[1], viewPortVals[2], viewPortVals[3]);
    }

    @Override
    public ViewPort getViewPort() {
        return viewPort;
    }

    @Override
    public Vector3f getDirection() {
        // TODO Auto-generated method stub
        return cam.getDirection();
    }

    @Override
    public void reset() {
        // TODO Auto-generated method stub
    }
}
